<?php

class TypeEntiteDAO extends DAO{


	function getById($id){
		try
		{
			$id=Validator::escapeSql($id);
			$query="select * from TYPE_ENTITE where id=$id;";
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			
			$propDAO = $this->factory->getProprieteDAO();
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = new TypeEntite((int)$Bean->ID);
				$thebean->setDefault($Bean->IS_DEFAULT);
				$thebean->setLabel($Bean->LABEL);
				$thebean->setTooltip($Bean->TOOLTIP);
				$thebean->setProprietes($propDAO->getAllByTypeEntite($thebean));
			}
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $thebean;
	}
	function remove($Bean){
		try{
			$propDAO = $this->factory->getProprieteDAO();
			foreach ($Bean->getProprietes() as $Prop){
				$propDAO->remove($Prop);
			}
			$query="delete from TYPE_ENTITE where id='".Validator::escapeSql($Bean->getId())."';";
			$this->pdo->exec($query);
			$bool=true;
		}catch(Exception $e)    {
			$bool=false;
		}
		return $bool;

	}
	function update($Bean){
		try{
			$propDAO = $this->factory->getProprieteDAO();
			foreach ($Bean->getProprietes() as $Prop){
				$propDAO->update($Prop);
			}
			$query = "update TYPE_ENTITE set is_default=".Validator::escapeSql($Bean->isDefault()).", LABEL='".Validator::escapeSql($Bean->getLabel())."' , TOOLTIP='".Validator::escapeSql($Bean->getTooltip())."' where id=".Validator::escapeSql($Bean->getId()).";";
			$this->pdo->exec($query);
			$bool=true;
		}
		catch(Exception $e)     {
			$bool=false;

		}
		return $bool;
	}
	function add($Bean){
		try{
			 
			if (is_null($Bean->getId())){
				
				$propDAO = $this->factory->getProprieteDAO();
				foreach ($Bean->getProprietes() as $Prop){
					$propDAO->add($Prop);
				}
				$thebean = null;
				$insertQuery = "insert into TYPE_ENTITE (is_default,LABEL,TOOLTIP) values (".Validator::escapeSql($Bean->isDefault()).",'".Validator::escapeSql($Bean->getLabel())."','".Validator::escapeSql($Bean->getTooltip())."');";
				$this->pdo->exec($insertQuery);
				$thebean = $this->getById((int)$this->pdo->lastInsertId());
			}
		}catch(Exception $e)    {$this->pdo->message;}
		return $thebean;
	}

	function All(){
		try
		{

			$BeanArray = array();
			$query="select * from TYPE_ENTITE;";
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = $this->getById((int)$Bean->ID);
				array_push($BeanArray,$thebean);
			}
				
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $BeanArray;

	}
}
?>